# encoding=utf8
import sys
from JT_8103 import JT_8103
reload(sys)
sys.setdefaultencoding('utf8')

myclass=JT_8103()
class JT_0104:
    result = []
    analysisDescription=['应答流水号：','应答参数个数：','参数项列表：']
    analysis0104length=[2,1]
    par_list=myclass.par_list
    def LogicAisle(self, protocolStr, header):
        header.append(self.analysisDescription[0]+protocolStr[0:4]+"\n")
        par_total=int(protocolStr[4:6],16)
        header.append(self.analysisDescription[1]+str(par_total)+"\n")
        header.append(self.analysisDescription[2]+"\n")
        par_id_list=protocolStr[6:]
        s=par_id_list
        for i in range(par_total):
            find=False
            par_id_str=s[0:myclass.parid_len*2]
            print(par_id_str)
            par_len=s[myclass.parid_len*2:(myclass.parid_len+myclass.par_len_str_len)*2]
            print(par_len)
            par_len=int(par_len,16)
            for par in myclass.par_list:
                if "0000"+par['id'] == par_id_str.upper():
                    find=True
                    par_val=s[(myclass.parid_len+myclass.par_len_str_len)*2:(myclass.parid_len+myclass.par_len_str_len+par_len)*2]
                    print(par_val)
                    par_des=par['des']
                    #时间，单位秒
                    if par['id']=="0001" or par['id']=="0002" or par['id']=="0004" or par['id']=="0006" or \
                            par['id']=="0022" or par['id']=="0027" or par['id']=="0028" or par['id']=="0029" or \
                            par['id']=="0056" or par['id']=="0057" or par['id']=="0058" or par['id']=="0059" or \
                            par['id'] == "005A" or par['id']=="005C" or par['id']=="0093":
                        par_val=myclass.hex_to_dec(par_val)
                        header.append(par_des+": "+par_val+"s\n")
                    #通话时间
                    elif par['id']=="0046" or par['id']=="0047":
                        if par_val=="00000000":
                            header.append(par_des+": "+"不允许通话"+"\n")
                        elif par_val=="ffffffff":
                            header.append(par_des+": "+"不限制"+"\n")
                        else:
                            par_val=myclass.hex_to_dec(par_val)
                            header.append(par_des+": "+par_val+"s\n")
                    #tcp/udp 端口
                    #角度
                    #次数
                    #图像/视频质量，亮度，对比度，饱和度，色度
                    #车辆省域ID，车辆市域ID，车牌颜色
                    elif par['id']=="0003" or par['id']=="0005" or par['id']=="0007" or par['id']=="0018" or \
                            par['id']=="0019" or par['id']=="001B" or par['id']=="001C" or par['id']=="0030" or \
                            par['id']=="005E" or par['id']=="0070" or par['id']=="0071" or par['id']=="0072" or \
                            par['id']=="0073" or par['id']=="0074" or par['id']=="0081" or par['id']=="0082" or \
                            par['id']=="0084" :
                        par_val = myclass.hex_to_dec(par_val)
                        header.append(par_des + ": " + par_val + "\n")
                    #GBK解码(电话,机动车牌号)
                    elif par['id']=="0010" or par['id']=="0011" or par['id']=="0012" or par['id']=="0013" or \
                            par['id']=="0014" or par['id']=="0015" or par['id']=="0016" or par['id']=="0017" or \
                            par['id']=="001A" or par['id']=="001B" or par['id']=="001C" or par['id']=="001D" or \
                            par['id']=="0040" or par['id']=="0041" or par['id']=="0042" or par['id']=="0043" or \
                            par['id']=="0044" or par['id']=="0048" or par['id']=="0049" or par['id']=="0083" :
                        par_val = par_val.decode("hex").decode('gb2312')
                        header.append(par_des + ": " + par_val + "\n")
                    #位置汇报策略
                    elif par['id']=="0020":#0：定时汇报；1：定距汇报；2：定时和定距汇报
                        par_val=int(par_val,16)
                        if par_val==0:
                            header.append(par_des + ": " + str(par_val) + " 定时汇报" + "\n")
                        elif par_val==1:
                            header.append(par_des + ": " + str(par_val) + " 定距汇报" + "\n")
                        elif par_val==2:
                            header.append(par_des + ": " + str(par_val) + " 定时汇报和定距汇报" + "\n")
                    # 位置汇报方案
                    elif par['id']=="0021":#，0：根据 ACC 状态； 1：根据登录状态和 ACC 状态，先判断登录状态，若登录再根据 ACC 状态
                        par_val = int(par_val, 16)
                        if par_val==0:
                            header.append(par_des + ": " + str(par_val) + " 根据ACC状态" + "\n")
                        elif par_val==1:
                            header.append(par_des + ": " + str(par_val) + " 根据ACC状态和登录状态" + "\n")
                    #距离，米
                    elif par['id']=="002C" or par['id']=="002D" or par['id']=="002E" or par['id']=="002F" or \
                            par['id']=="0031" :
                        par_val=myclass.hex_to_dec(par_val)
                        header.append(par_des+": "+par_val+"m\n")
                    #终端电话接听策略
                    elif par['id']=="0045":
                        par_val = int(par_val, 16)
                        if par_val == 0:
                            header.append(par_des + ": " + str(par_val) + " 自动接听" + "\n")
                        elif par_val == 1:
                            header.append(par_des + ": " + str(par_val) + " ACC ON时自动接听" + "\n")
                        elif par_val == 2:
                            header.append(par_des + ": " + str(par_val) + " OFF时手动接听" + "\n")
                    #报警标志
                    elif par['id']=="0050" or par['id']=="0051" or par['id']=="0052" or par['id']=="0053" or par['id']=="0054" :
                        par_val=myclass.hex_to_bin(par_val,par_len*8)
                        print(par_val)
                        header.append(par_des + ": "  + par_val + "\n")
                        myclass.set_alarm_num(par_val, header)
                    #速度
                    elif par['id']=="0055":#km/h
                        par_val=int(par_val,16)
                        header.append(par_des+": "+ str(par_val) + "km/h\n")
                    elif par['id']=="005B" or par['id']=="0080" :#o.1km/h
                        par_val = int(par_val, 16)*0.1
                        header.append(par_des + ": " + str(par_val) + "km/h\n")
                    #碰撞报警参数
                    elif par['id']=="005D":
                        #碰撞时间
                        collision_t=int(par_val[2:],16)*4
                        #碰撞加速度
                        collision_a=int(par_val[0:2],16)*0.1
                        header.append(par_des + ": " + "碰撞时间："+ str(collision_t) + "ms 碰撞加速度：" + str(collision_a) +"g\n")
                    #定时拍照标志
                    elif par['id']=="0064":
                        par_val=myclass.hex_to_bin(par_val,par_len*8)
                        header.append(par_des+": "+ par_val +"\n")
                        myclass.set_time_photo_control(par_val,header)
                    # 定距拍照标志
                    elif par['id'] == "0065":
                        par_val = myclass.hex_to_bin(par_val,par_len*8)
                        header.append(par_des + ": " + par_val + "\n")
                        myclass.set_disten_photo_control(par_val, header)
                    #GNSS定位模式
                    elif par['id']=="0090":
                        par_val=myclass.hex_to_bin(par_val,par_len*8)
                        count=8
                        header.append(par_des+": "+par_val+"\n")
                        for k in range(count):
                            if k>3:
                                pass
                            else:
                                gnss_bit=par_val[count-1:count]
                                if gnss_bit=="1":
                                    header.append("启用"+myclass.gnss_mode[k]+"\n")
                                elif gnss_bit=="0":
                                    header.append("禁用" + myclass.gnss_mode[k] + "\n")
                            count -=1
                    #GNSS波特率
                    elif par['id']=="0091":
                        msg=""
                        if par_val=="00":
                            msg="4800"
                        elif par_val=="01":
                            msg="9600"
                        elif par_val=="02":
                            msg="19200"
                        elif par_val=="03":
                            msg="38400"
                        elif par_val=="04":
                            msg="57600"
                        elif par_val=="05":
                            msg="115200"
                        header.append(par_des+": "+msg+"\n")
                    #GNSS模块详细定位数据输出频率
                    elif par['id']=="0092":
                        msg=""
                        if par_val=="00":
                            msg="500ms"
                        elif par_val=="01":
                            msg="1000ms(默认值）"
                        elif par_val=="02":
                            msg="2000ms"
                        elif par_val=="03":
                            msg="3000ms"
                        elif par_val=="04":
                            msg="4000ms"
                        header.append(par_des+": "+msg+"\n")
                    #GNSS模块详细定位数据上传方式
                    elif par['id']=="0094":
                        myclass.gnss_uplode = par_val
                        msg=""
                        if par_val=="00":  msg="本地存储，不上传（默认值）"
                        elif par_val=="01": msg="按时间间隔上传"
                        elif par_val=="02": msg="按距离间隔上传"
                        elif par_val.upper()=="0B": msg="按累计时间上传，达到传输时间后自动停止上传"
                        elif par_val.upper()=="0C": msg="按累计距离上传，达到距离后自动停止上传"
                        elif par_val.upper() == "0D": msg = "按累计条数上传，达到上传条数后自动停止上传"
                        header.append(par_des+": "+msg+"\n")
                    # GNSS模块详细定位数据上传设置
                    elif par['id'] == "0095":
                        par_val=myclass.hex_to_dec(par_val)
                        msg=""
                        if myclass.gnss_uplode == "01":
                            msg = par_val+"s"
                        elif myclass.gnss_uplode == "02":
                            msg = par_val+"m"
                        elif myclass.gnss_uplode.upper() == "0B":
                            msg = par_val+"s"
                        elif myclass.gnss_uplode.upper() == "0C":
                            msg = par_val+"m"
                        elif myclass.gnss_uplode.upper() == "0D":
                            msg = par_val+"条"
                        header.append(par_des + ": " + msg + "\n")
                    #CAN采集时间间隔，时间，ms
                    elif par['id']=="0100" or par['id']=="0102" :
                        par_val=myclass.hex_to_dec(par_val)
                        header.append(par_des+": "+par_val+"ms\n")
                    #CAN总线ID单独采集设置
                    elif par['id']=="0110":
                        par_val=myclass.hex_to_bin(par_val,par_len*8)
                        myclass.set_CAN_id(par_val,header)
                    else:
                        header.append(par_des+": "+par_val+"\n")
                    s=s[(myclass.parid_len+myclass.par_len_str_len+par_len)*2:]
            if find==False:
                header.append(par_id_str+"\n")

        self.result=header
